package com.wyy.filter;

import com.wyy.bean.Users;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @ClassName LoginFilter
 * @Description //TODO
 * @Author wky
 * @Date 2021/9/21 14:41
 * @Version 1.0
 **/
public class LoginFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
    
    }
    
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse)servletResponse;
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        Users user = (Users) request.getSession().getAttribute("user1");
        //如果用户信息不存在，3s后跳转至登录页面
        if (user==null||user.getLoginName().length()==0){
            out.println("您还未登陆，3秒钟后跳转至登录页面");
            response.setHeader("refresh","3;/login.jsp");
        }
        else{
            filterChain.doFilter(servletRequest, servletResponse);
        }
        
    }
    
    @Override
    public void destroy() {
    
    }
}
